package com.guohui.boot1.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.guohui.boot1.controller.api.IUserController;
import com.guohui.boot1.entity.DemoUser;
import com.guohui.boot1.service.DemoUserService;
import com.guohui.boot1.util.cache.RedisCache;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/users")
public class UserController implements IUserController{

  @Autowired
  private DemoUserService demoUserService;
  
	@ApiOperation(value="获取用户列表")
	@RequestMapping(method=RequestMethod.GET)
	public List<DemoUser> getUsers(){
		
	  
		List<DemoUser> list = this.demoUserService.findList();
		
		return list;
	}
	
	@ApiOperation(value="获取用户",notes="根据用户ID获取用户")
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public DemoUser getUser(@PathVariable Long id){
		
		DemoUser user = this.demoUserService.getById(id);
		RedisCache.set("user:"+user.getId(), user);
		return user;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String addUser(@ModelAttribute DemoUser user){
		
		this.demoUserService.save(user);
		
		return "success";
	}
	
	@ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
	@RequestMapping(value="/update/{id}",method=RequestMethod.POST)
	public String updateUser(@PathVariable Long id,@RequestBody DemoUser user){
		
	  DemoUser u = this.demoUserService.getById(id);
		u.setMobile(user.getMobile());
		u.setName(user.getName());
		u.setDelFlag(user.getDelFlag());
		this.demoUserService.updateById(u);
		return "success";
	}
	
	@ApiIgnore
	@RequestMapping("/del/{id}")
	public String delUser(@PathVariable Long id){
		
	  this.demoUserService.removeById(id);
		return "success";
	}
	
	@RequestMapping(value="/clear",method=RequestMethod.GET)
	public String clear(){
		
	  
		return "success";
	}
	
	
}
